package com.wang.controller;

import com.wang.domain.Student;
import com.wang.exception.AgeException;
import com.wang.exception.NameException;
import com.wang.exception.StudentException;
import com.wang.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService service;
    @RequestMapping("/addStudent.do")
    public ModelAndView addStudent(Student student) throws StudentException {
        String tips = "失败";
        ModelAndView mv = new ModelAndView();
        String name = student.getName();
        for (int i = 0; i < name.length(); i++) {
            if (!StudentController.isChineseChar(name.charAt(i))){
                throw new NameException("姓名不正确");
            }
        }
        if (student.getAge()==null||student.getAge()>80){
            throw new AgeException("年龄不正确");
        }
        int nums = service.addStudent(student);
        if (nums>0)tips = "学生:"+student.getName()+"添加成功";
        mv.addObject("tips",tips);
        mv.setViewName("result");
        return mv;
    }
    @RequestMapping("/queryStudent.do")
    @ResponseBody
    public List<Student> queryStudent(){
        //检查参数
        List<Student> students = service.findStudents();
        return students;
    }
    @RequestMapping("/login.do")
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response,Student student) throws StudentException {
        String tips = "登录失败";
        ModelAndView mv = new ModelAndView();
        String name = student.getName();
        for (int i = 0; i < name.length(); i++) {
            if (!StudentController.isChineseChar(name.charAt(i))){
                throw new NameException("姓名不正确");
            }
        }
        if (student.getAge()==null||student.getAge()>80){
            throw new AgeException("年龄不正确");
        }
        //判断是否存在用户,存在则发放session
        List<Student> studentsById = service.findStudentsById(student);
        if (studentsById.size()==0){
            //说明没有这个用户,
            request.setAttribute("errormsg","您需要先注册~");
        }
        else{
            request.getSession().setAttribute("name","1");
            tips = "登录成功";
        }
        mv.addObject("tips",tips);
        mv.setViewName("result");
        return mv;
    }

    @RequestMapping("/unregist.do")
    public String unregist(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("name");
        request.setAttribute("tips","注销成功!");
        return "result";
    }
    public static boolean isChineseChar(char c) {
        try {
            return String.valueOf(c).getBytes("UTF-8").length > 1;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
